package com.example.mypra1.adapter;
import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.mypra1.R;
import com.example.mypra1.adapter.entity.Comment;
import com.ldoublem.thumbUplib.ThumbUpView;
import com.szysky.customize.siv.SImageView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public abstract class MyListAdapter extends ArrayAdapter<Comment> {
    private int resourceId;
    public MyListAdapter(@NonNull Context context, int resourceId, @NonNull List<Comment> objects) {
        super(context, resourceId, objects);
        this.resourceId = resourceId;
    }
    public abstract void showReply(int position);

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Comment comment = getItem(position);
        String urls[] = comment.getImgs().split("&");
        View view= LayoutInflater.from(getContext()).inflate(resourceId,null);
        ThumbUpView tpv = (ThumbUpView) view.findViewById(R.id.tpv);
        TextView favor = (TextView) view.findViewById(R.id.favor);
        favor.setText(String.valueOf(comment.getFavor()));
        tpv.setOnThumbUp(new ThumbUpView.OnThumbUp() {
            @Override
            public void like(boolean like) {
                if (like==true){
                    int counts = comment.getFavor()+1;
                    comment.setFavor(counts);
                    favor.setText(String.valueOf(counts));
                    modifyFavor(1,comment.getId());

                }else{
                    int counts = comment.getFavor()-1;
                    comment.setFavor(counts);
                    favor.setText(String.valueOf(counts));
                    modifyFavor(-1,comment.getId());
                }
            }
        });
        SImageView profile=(SImageView) view.findViewById(R.id.profile);
        TextView pubname=(TextView) view.findViewById(R.id.pubname);
        TextView pubtime = (TextView) view.findViewById(R.id.pubtime);
        TextView content = (TextView) view.findViewById(R.id.content);
        GridView imgs = (GridView) view.findViewById(R.id.imgs);
        imgs.setAdapter(new MyGridAdapter(urls,getContext()));
        TextView rep_count = (TextView) view.findViewById(R.id.rep_count);
        ImageView rep = (ImageView)view.findViewById(R.id.go_reply);
        rep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              showReply(position);
            }
        });
        profile.setImageUrls(comment.getProfile());
        pubname.setText(comment.getPubname());
        pubtime.setText(comment.getPubtime());
        content.setText(comment.getContent());
        rep_count.setText(String.valueOf(comment.getRepcount()));
        return view;
    }
    private void modifyFavor(int like,int cmtid){//like取值为1或-1
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://8.142.19.67:3000/addFavor?cmtid="+cmtid+"&num="+like)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                Log.d("resp",response.toString());
                if (response.isSuccessful()) {
                    String res = response.body().string();
                    Log.d("res",res);
                    try {
                        JSONObject jsonObject = new JSONObject(res);
                        int code = jsonObject.getInt("code");
                        if(code==200){
                            Log.d("res","点赞成功!");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                }
            }
        }


});}


}
